package com.rongc.client.core.utils;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class StringUtils {
    public static final String EMPTY = "";
    public static final String SPACE = " ";

    /**
     * 将字符串转double
     *
     * @param value
     * @return
     */
    public static double parseDouble(String value) {
        try {
            return Double.parseDouble(value);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static float parseFloat(String value) {
        try {
            return Float.parseFloat(value);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static int parseInt(String value) {
        try {
            if(isEmpty(value)) return 0;
            return Integer.parseInt(value);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static long parseLong(String value) {
        try {
            if(isEmpty(value)) return 0;
            return Long.parseLong(value);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static int charLength(String paramString) {
        if (isEmpty(paramString)) {
            return 0;
        }
        char[] arrayOfChar = paramString.toCharArray();
        int k = 0;
        for (int i = 0; i < arrayOfChar.length; i++) {
            int m = arrayOfChar[i];
            if (m < 0 || m > 127) {
                k += 2;
                continue;
            }
            k++;
        }
        return k;
    }

    public static String forceEllipsizeEnd(String paramString, int length) {
        if (paramString.length() > length) {
            paramString = paramString.substring(0, length) + "...";
        }
        return paramString;
    }

    public static boolean isBlank(String paramString) {
        if (paramString != null) {
            if (paramString.length() != 0) {
                for (int i = 0; i < paramString.length(); i++) {
                    if (!Character.isWhitespace(paramString.charAt(i))) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public static boolean isEmpty(String paramString) {
        if (/*("null".equals(paramString)) || */(paramString == null) || (paramString.length() == 0)) {
            return true;
        }
        return false;
    }

    public static boolean isNotBlank(String paramString) {
        if (!isBlank(paramString))
            return true;
        return false;
    }

    public static boolean isNotEmpty(String paramString) {
        if (!isEmpty(paramString))
            return true;
        return false;
    }

    public static String makeSerial(int num){
        DecimalFormat df = new DecimalFormat("000");
        return df.format(num);
    }

    public static String makeGdPeriod(int num){
        DecimalFormat df = new DecimalFormat("00");
        return df.format(num);
    }

    public static String makeDistance(double num){
        DecimalFormat df = new DecimalFormat("###.0");
        return df.format(num);
    }

    public static String makePercent(double num){
//        DecimalFormat df = new DecimalFormat("##.00%");
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMinimumFractionDigits(2);
        return nf.format(num / 100);
    }

    public static String makeBonus(double num){
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);
        return df.format(num);
    }

    public static String makePwd(String paramString,int startIndex_,int endIndex_) {
        if(isEmpty(paramString)) return "****";

        int length = paramString.length();
        if(startIndex_ > length) startIndex_ = length / 2 - length / 4;
        if(endIndex_ > length) endIndex_ = length / 2 + endIndex_ / 4;
        String temp = "";
        for (int i = 0;i< startIndex_ - endIndex_;i++){
            temp += "*";
        }

        return paramString.substring(0,startIndex_) + temp + paramString.substring(endIndex_,paramString.length());
    }

    public static String toNormalNum(String string){
        NumberFormat nf = new DecimalFormat(",##0.00");
        return nf.format(StringUtils.parseDouble(string));
    }

    public static String toChinese(String string) {
        String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };

        String result = "";

        int n = string.length();
        for (int i = 0; i < n; i++) {

            int num = string.charAt(i) - '0';

            if (i != n - 1 && num != 0) {
                result += s1[num] + s2[n - 2 - i];
            } else {
                result += s1[num];
            }
            System.out.println("  "+result);
        }

        return result;

    }
}
